/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.mall.member.web.MyBankAction
   Module Description   :

   Date Created      : 2010/1/12
   Original Author   : feeling.wang
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.mall.member.web;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.ec.member.entity.Member;
import com.yaodian100.ec.member.entity.VirtualBalance;
import com.yaodian100.ec.member.service.MemberService;
import com.yaodian100.mall.member.util.UserData;

/**
 * @author feeling.wang
 * 
 */
public class MyBankAction extends ActionSupport implements Preparable {

	private static final long serialVersionUID = 3698956884775066567L;
	/** logger */
	private final Logger logger = LoggerFactory.getLogger(MyBankAction.class); // 與 getter, setter

	private SimplePager pager; // 與 setter
	private MemberService memberService;
	private UserData userData = null;
	private CommonCriteria crit;
	private Member member;
	private long virtualBalanceSize;
	private List<VirtualBalance> virtualBalanceList;
	private String virtualType; // 0:存入,1:支付,2:提现

	/**
	 * 账户余额
	 * 
	 * @return
	 * @throws Exception
	 */
	public String myBank() throws Exception {
		member = memberService.get(userData.getMemberId());
		crit = new CommonCriteria();
		crit.addEq("memberId", userData.getMemberId());
		crit.addEq("type", "V");
		logger.info("virtualType :{} ", virtualType);
		if (StringUtils.isNotBlank(virtualType)) {
			if (StringUtils.equalsIgnoreCase(virtualType, "0")) {
				crit.addGe("trxAmount", new BigDecimal(0));
			} else if (StringUtils.equalsIgnoreCase(virtualType, "1")) {
				crit.addLe("trxAmount", new BigDecimal(0));
				crit.addNe("trxType", "EC_APPLY_CASH_OUT");
			} else if (StringUtils.equalsIgnoreCase(virtualType, "2")) {
				crit.addEq("trxType", "EC_APPLY_CASH_OUT");
				crit.addLe("trxAmount", new BigDecimal(0));
			}
		}

		virtualBalanceSize = memberService.getVirtualBalanceSize(crit).longValue();
		pager.setPageRecord(20);// defaut 20
		pager.setTotalSize(virtualBalanceSize);
		virtualBalanceList = memberService.getVirtualBalanceList(pager.getCurrentPage() * pager.getPageRecord(), pager
				.getPageRecord(), crit, new String[] { "id" });
		return "myBank";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.Preparable#prepare()
	 */

	public void prepare() throws Exception {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		userData = new UserData(request, response);
		if (null == pager)
			pager = new SimplePager(1);

	}

	public void setMember(Member member) {
		this.member = member;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public void setVirtualBalanceList(List<VirtualBalance> virtualBalanceList) {
		this.virtualBalanceList = virtualBalanceList;
	}

	public void setVirtualBalanceSize(long virtualBalanceSize) {
		this.virtualBalanceSize = virtualBalanceSize;
	}

	public void setVirtualType(String virtualType) {
		this.virtualType = virtualType;
	}

	public Member getMember() {
		return member;
	}

	public SimplePager getPager() {
		return pager;
	}

	public List<VirtualBalance> getVirtualBalanceList() {
		return virtualBalanceList;
	}

	public long getVirtualBalanceSize() {
		return virtualBalanceSize;
	}

	public String getVirtualType() {
		return virtualType;
	}
}